# -*- coding: utf-8 -*-
# @Time    : 2018/2/9 14:09
# @Author  : Soft
# @File    : service.py
# @Desc    : 签到记录service层
from module.models import Record, db, Page
from config import Check
import logging

log = logging.getLogger('Record Service LOG')


class ReService:
    def __init__(self):
        pass

    @staticmethod
    @db.atomic()
    def add(**record):
        return Record.insert(**record).execute()

    @staticmethod
    @db.atomic()
    def remove(rid=0):
        return Record.delete_by_id(rid)

    @staticmethod
    def change(rid=0, **record):
        try:
            with db.atomic():
                Record.update(**record).where(Record.id == rid).execute()
                return True
        except BaseException as e:
            log.debug('修改失败--> %s' % e)
            return False

    @staticmethod
    def record_count(year=None, sid=0):
        """
        统计某个学生的出勤和迟到总记录数 未用
        :param year:
        :param sid:
        :return:
        """
        try:
            if year:
                return Record.select().where(Record.rd_time.startswith(year),
                                             Record.stu == sid,
                                             Record.rd_status.in_([Check.CQ, Check.CD])).count()
            return Record.select().count()
        except BaseException as e:
            log.debug('统计签到记录失败--> %s' % e)
            return 0

    @staticmethod
    def is_has(kid=0, sid=0):
        try:
            record = Record.select().where(Record.k == kid, Record.stu == sid).get()
            return True, record.to_dict()
        except BaseException as e:
            log.debug('通过sid kid查询失败 无记录--> %s' % e)
            return False, Record().to_dict()

    @staticmethod
    def pages(page=1, size=12, year='2018', k_id=None, stu_id=None, status=None):
        result = None
        if k_id and not stu_id:
            if status and int(status) >= 0:
                result = Record.select(). \
                    where(Record.k == k_id, Record.rd_time.startswith(year), Record.rd_status==status). \
                    order_by(Record.k.desc()). \
                    paginate(page, size)
                Page.total = Record.select(). \
                    where(Record.k == k_id, Record.rd_time.startswith(year), Record.rd_status==status).count()
            else:
                result = Record.select(). \
                    where(Record.k == k_id, Record.rd_time.startswith(year)). \
                    order_by(Record.k.desc()). \
                    paginate(page, size)
                Page.total = Record.select().\
                    where(Record.k == k_id, Record.rd_time.startswith(year)).count()
        if stu_id and not k_id:
            if status and int(status) >= 0:
                result = Record.select(). \
                    where(Record.stu == stu_id, Record.rd_time.startswith(year), Record.rd_status==status). \
                    order_by(Record.k.desc()). \
                    paginate(page, size)
                Page.total = Record.select(). \
                    where(Record.stu == stu_id, Record.rd_time.startswith(year), Record.rd_status==status).count()
            else:
                result = Record.select(). \
                    where(Record.stu == stu_id, Record.rd_time.startswith(year)). \
                    order_by(Record.k.desc()). \
                    paginate(page, size)
                Page.total = Record.select(). \
                    where(Record.stu == stu_id, Record.rd_time.startswith(year)).count()
        if k_id and stu_id:
            if status and int(status) >= 0:
                result = Record.select(). \
                    where(Record.k == k_id, Record.stu == stu_id, Record.rd_time.startswith(year),
                          Record.rd_status == status). \
                    order_by(Record.k.desc()). \
                    paginate(page, size)
                Page.total = Record.select(). \
                    where(Record.k == k_id, Record.stu == stu_id, Record.rd_time.startswith(year),
                          Record.rd_status==status).count()
            else:
                result = Record.select(). \
                    where(Record.k == k_id, Record.stu == stu_id, Record.rd_time.startswith(year)). \
                    order_by(Record.k.desc()). \
                    paginate(page, size)
                Page.total = Record.select(). \
                    where(Record.k == k_id, Record.stu == stu_id, Record.rd_time.startswith(year)).count()
        if not k_id and not stu_id:
            if status and int(status) >= 0:
                result = Record.select(). \
                    where(Record.rd_time.startswith(year), Record.rd_status==status). \
                    order_by(Record.k.desc()). \
                    paginate(page, size)
                Page.total = Record.select(). \
                    where(Record.rd_time.startswith(year), Record.rd_status==status).count()
            else:
                result = Record.select(). \
                    where(Record.rd_time.startswith(year)). \
                    order_by(Record.k.desc()). \
                    paginate(page, size)
                Page.total = Record.select(). \
                    where(Record.rd_time.startswith(year)).count()
        Page.result = []
        for re in result:
            try:
                Page.result.append(re.to_dict())
            except BaseException as e:
                log.debug('记录中的课程或用户不存在--> %s' % e)
                Page.total -= 1
                continue
        Page.page = page
        Page.limit = size
        return Page.to_dict()

        # print ReService.record_count('2018', 6)
